package com.sdonkey.score.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdonkey.score.dao.AdConditionDao;
import com.sdonkey.score.model.AdPlacementConditionListBean;
import com.sdonkey.score.model.AdPlacementGiftListBean;
import com.sdonkey.score.model.FinishPlacementAdBean;
import com.sdonkey.score.service.AdPlaceConditionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by hujun on 2018\12\29 0029.
 */
@Service("adPlaceConditionService")
@Transactional
public class AdPlaceConditionServiceImpl implements AdPlaceConditionService {
    @Autowired
    AdConditionDao adConditionDao;

    @Override
    public List<FinishPlacementAdBean> getUnConditonAdList(String advId) {
        return adConditionDao.getUnConditionPlacementAd(advId);
    }

    @Override
    public JSONObject getAdPlaceConditionById(int adId) {
        Map adCondition = adConditionDao.getAdPlaceConditionByAdId(adId);
        if (adCondition == null) {
            return null;
        }
        if (adCondition.containsKey("birth")) {
            String birth = (String) adCondition.get("birth");
            String[] births = birth.split(",");
            adCondition.put("birth", births);
        }
        if (adCondition.containsKey("education")) {
            String education = (String) adCondition.get("education");
            String[] educations = education.split(",");
            adCondition.put("education", educations);
        }
        if (adCondition.containsKey("monthlyIncome")) {
            String monthlyIncome = (String) adCondition.get("monthlyIncome");
            String[] monthlyIncomes = monthlyIncome.split(",");
            adCondition.put("monthlyIncome", monthlyIncomes);
        }
        JSONObject result = JSONObject.parseObject(JSON.toJSONString(adCondition));
        return result;
    }

    @Override
    public Integer setAdPlaceCondition(JSONObject params) {
        if (params.containsKey("birth")) {
            JSONArray birth = params.getJSONArray("birth");
            if(birth == null || birth.size() == 0){
                params.remove("birth");
            }else {
                params.put("birth", arrayToStrWithComma(params.getJSONArray("birth").toArray(new String[]{})));
            }

        }
        if(params.containsKey("education")){
            JSONArray education = params.getJSONArray("education");
            if(education == null || education.size() == 0){
                params.remove("education");
            }else {
                params.put("education", arrayToStrWithComma(params.getJSONArray("education").toArray(new String[]{})));
            }
        }
        if (params.containsKey("monthlyIncome")) {
            JSONArray monthlyIncome = params.getJSONArray("monthlyIncome");
            if(monthlyIncome == null || monthlyIncome.size() == 0){
                params.remove("monthlyIncome");
            }else {
                params.put("monthlyIncome", arrayToStrWithComma(params.getJSONArray("monthlyIncome").toArray(new String[]{})));
            }
        }
        Map<String, Object> itemMap = JSONObject.toJavaObject(params, Map.class);
        if (itemMap.containsKey("id")) {//修改
            adConditionDao.updatePlaceConditionById(itemMap);
        } else {//添加
            itemMap.put("status", 1);
            adConditionDao.addAdPlaceCondition(itemMap);
        }
        return (Integer) itemMap.get("id");
    }

    @Override
    public List<AdPlacementConditionListBean> getAdPlaceConditionList(String advId) {
        if (StringUtils.isBlank(advId)) {
            return null;
        }
        List<AdPlacementConditionListBean> list = adConditionDao.getAdConditionList(advId);
        return list;
    }

    @Override
    public void deleteAdPlaceCondition(int id) {
        adConditionDao.deleteById(id);
    }
    /**
     * 将数组转换成以逗号分隔的字符串
     *
     * @param needChange 需要转换的数组
     * @return 以逗号分割的字符串
     */
    public static String arrayToStrWithComma(String[] needChange) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < needChange.length; i++) {
            sb.append(needChange[i]);
            if ((i + 1) != needChange.length) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

}
